package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.service.impl.BiAccessoryButtonServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiAccessoryButtonServlet extends AbstractBaseServlet {
	private static final long serialVersionUID = -8209339914606360762L;
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 10;

	private BiAccessoryButton biAB;

	private BiAccessoryButtonServiceImpl abService;

	public BiAccessoryButtonServlet() {
		super();
		this.abService = new BiAccessoryButtonServiceImpl();
		this.abService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.fullName = BiAccessoryButton.class.getName();
		this.simpleName = BiAccessoryButton.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.abService.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		super.setAcUser(request, this.abService);
		String rtn = "";
		switch (this.action) {
		case ACTION_GET: // "get one"
			super.setId(request);
			rtn = this.abService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			super.setId(request);
			rtn = this.abService.getWithDescription(id);
			break;
		case ACTION_DELETE:
			super.setId(request);
			rtn = this.abService.delete(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER:
			super.setPaginationParam(request);
			super.setFilterParam(request);
			rtn = this.abService.getAllPagination(this.page, this.sort,
					this.dir, super.filters);
			break;
		case ACTION_EDIT:
			this.fillObj(request);
			rtn = this.abService.edit(this.biAB);
			break;
		case ACTION_GET_MAX_REFERENCE_INDEX:
			String ctMaterialType = (String) this.getValue(request, "ctMaterialType");
			String ctShape = (String) this.getValue(request, "ctShapeType");
			String holeQuantity = (String) this.getValue(request, "holeQuantity");
			String measure = (String) this.getValue(request, "measure");
			rtn = this.abService.getMaxReferenceIndex(ctMaterialType, ctShape, Integer.parseInt(holeQuantity), Integer.parseInt(measure));
			break;
		}
		return rtn;
	}

	private void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.biAB = (BiAccessoryButton) this.obj;

		String fcId = (String) getValue(request, "customerId");
		if (!StringUtils.isEmpty(fcId)) {
			BiFranceCustomer biFc = new BiFranceCustomer();
			biFc.setId(Integer.parseInt(fcId));
			this.biAB.setBiFranceCustomer(biFc);
		}

		String fId = (String) getValue(request, "factoryId");
		if (!StringUtils.isEmpty(fId)) {
			BiFactory biF = new BiFactory();
			biF.setId(Integer.parseInt(fId));
			this.biAB.setBiFactory(biF);
		}
	}
}
